package com.zscat.mallplus.service.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.cms.entity.CmsFavorite;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.mapper.CmsFavoriteMapper;
import com.zscat.mallplus.entity.vo.oms.EvaluationOrder;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.service.cms.service.ICmsFavoriteService;
import com.zscat.mallplus.service.cms.service.ICmsSubjectService;
import com.zscat.mallplus.vo.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-06-15
 */
@Service
public class CmsFavoriteServiceImpl extends ServiceImpl<CmsFavoriteMapper, CmsFavorite> implements ICmsFavoriteService {

    @Resource
    private CmsFavoriteMapper productCollectionRepository;
    @Resource
    private PmsProductMapper productMapper;
    @Resource
    private ICmsSubjectService subjectService;

    @Override
    public int addProduct(CmsFavorite productCollection) {
        CmsFavorite query = new CmsFavorite();
        query.setObjId(productCollection.getObjId());
        query.setMemberId(productCollection.getMemberId());
        query.setType(productCollection.getType());
        CmsFavorite findCollection = productCollectionRepository.selectOne(new QueryWrapper<>(query));
        int count = 0;
        if (findCollection == null) {
            productCollection.setAddTime(new Date());
            productCollectionRepository.insert(productCollection);
            if (productCollection.getType() == 1) {
                PmsProduct subject = productMapper.selectById(productCollection.getObjId());
                subject.setId(productCollection.getObjId());
                subject.setGiftPoint(subject.getGiftPoint() + 1);
                //更新到数据库
                productMapper.updateById(subject);
            }
            if (productCollection.getType() == 2) {
                CmsSubject subject = subjectService.getById(productCollection.getObjId());
                subject.setId(productCollection.getObjId());
                subject.setCollectCount(subject.getCollectCount() + 1);
                //更新到数据库
                subjectService.updateById(subject);
            }
            count = 1;
        } else {
            if (productCollection.getType() == 1) {
                PmsProduct subject = productMapper.selectById(productCollection.getObjId());
                subject.setId(productCollection.getObjId());
                subject.setGiftPoint(subject.getGiftPoint() - 1);
                //更新到数据库
                productMapper.updateById(subject);
            }
            if (productCollection.getType() == 2) {
                CmsSubject subject = subjectService.getById(productCollection.getObjId());
                subject.setId(productCollection.getObjId());
                subject.setCollectCount(subject.getCollectCount() - 1);
                //更新到数据库
                subjectService.updateById(subject);
            }
            return productCollectionRepository.delete(new QueryWrapper<>(query));
        }
        return count;
    }

    @Override
    public Integer addFavorite(Long memberId, Long productId) {

        PmsProduct pmsProduct = productMapper.selectById(productId);

        if (pmsProduct != null) {

            //如果等于空、说明该用户还未收藏该商品、反之不等于空、则不用收藏
            CmsFavorite favoriteGoods = findFavoriteGoods("member_id", memberId, "obj_id", productId);

            if (favoriteGoods == null) {

                CmsFavorite cmsFavorite = new CmsFavorite();

                //图片
                String pic = pmsProduct.getPic();
                //商品名称
                String name = pmsProduct.getName();

                Integer sale = pmsProduct.getSale();

                BigDecimal price = pmsProduct.getPrice();

                cmsFavorite.setType(1);
                cmsFavorite.setMeno1(pic);
                cmsFavorite.setName(name);
                cmsFavorite.setObjId(productId);
                cmsFavorite.setMemberId(memberId);
                cmsFavorite.setAddTime(new Date());
                cmsFavorite.setMeno3(String.valueOf(sale));
                cmsFavorite.setMeno2(String.valueOf(price));

                return productCollectionRepository.insert(cmsFavorite);

            }
        }
        return 0;
    }

    @Override
    public Integer removeOrcancelFavorite(Long memberId, String productId) {
        String[] split = productId.split(",");

        for (int i = 0; i < split.length; i++) {
            CmsFavorite favoriteGoods = findFavoriteGoods("member_id", memberId, "obj_id", split[i]);
            if (favoriteGoods != null) {
                 productCollectionRepository.deleteById(favoriteGoods.getId());
            }
        }
        return 0;
    }

    @Override
    public PageInfo<CmsFavorite> getFavoriteList(Long memberId, Integer pageSize, Integer pageNum) {
        List<CmsFavorite> favoreteList = findFavoreteList("member_id", memberId, "add_time", pageSize.longValue(), pageNum.longValue());
        return new PageInfo<>(pageSize, pageNum, favoreteList);

    }

    private List<CmsFavorite> findFavoreteList(String column, Object value, String value1, Long pageSize, Long pageNum) {
        IPage<CmsFavorite> page = new Page<>();
        page.setSize(pageNum);
        page.setPages(pageSize);

        QueryWrapper<CmsFavorite> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(column, value).orderByDesc(value1);
        return productCollectionRepository.selectPage(page, queryWrapper).getRecords();
    }


    private CmsFavorite findFavoriteGoods(String column, Object value, String column1, Object value1) {
        QueryWrapper<CmsFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, value).eq(column1, value1);
        return productCollectionRepository.selectOne(queryWrapper);
    }


    @Override
    public List<CmsFavorite> listProduct(Long memberId, int type) {
        return productCollectionRepository.selectList(new QueryWrapper<CmsFavorite>().eq("member_id", memberId).eq("type", type));
    }

    @Override
    public List<CmsFavorite> listCollect(Long memberId) {
        return productCollectionRepository.selectList(new QueryWrapper<CmsFavorite>().eq("member_id", memberId));
    }


}
